package com.laf.manager.controller;

import com.laf.manager.service.ActivityService;
import com.laf.manager.service.AssessmentService;
import com.laf.manager.service.CouponService;
import com.laf.manager.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class StatisticsController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponService couponService;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private AssessmentService assessmentService;

//  @GetMapping("/charts")
    @GetMapping("/")
    public ModelAndView getChartData() {

        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));

        List<Integer> membersData = memberService.getMemberCountMonthly(year);
        List<Integer> activityData = activityService.getActivityCountMonthly(year);
        List<Integer> couponData = couponService.getCouponCountMonthly(year);
        List<Integer> verfiData = couponService.getVerifCountMonthly(year);
        List<Integer> assessmentData = assessmentService.getAssessmentCountMonthly(year);

        String membersDataJson = "";
        for (int i = 0; i < membersData.size(); i++) {
            Integer data = membersData.get(i);
            membersDataJson += data + ((i+1) < membersData.size() ? "," : "");
        }

        String activityDataJson = "";
        for (int i = 0; i < activityData.size(); i++) {
            Integer data = activityData.get(i);
            activityDataJson += data + ((i+1) < activityData.size() ? "," : "");
        }

        String couponDataJson = "";
        for (int i = 0; i < couponData.size(); i++) {
            Integer data = couponData.get(i);
            couponDataJson += data + ((i+1) < couponData.size() ? "," : "");
        }

        String verfiDataJson = "";
        for (int i = 0; i < verfiData.size(); i++) {
            Integer data = verfiData.get(i);
            verfiDataJson += data + ((i+1) < verfiData.size() ? "," : "");
        }

        String assessmentDataJson = "";
        for (int i = 0; i < assessmentData.size(); i++) {
            Integer data = assessmentData.get(i);
            assessmentDataJson += data + ((i+1) < assessmentData.size() ? "," : "");
        }

        ModelAndView model = new ModelAndView();

        model.setViewName("datachart");

        model.getModel().put("membersData", membersDataJson);
        model.getModel().put("activityData", activityDataJson);
        model.getModel().put("couponData", couponDataJson);
        model.getModel().put("verfiData", verfiDataJson);
        model.getModel().put("assessmentData", assessmentDataJson);

        return model;
    }


    @GetMapping("/chartsnew")
    @ResponseBody
    public Map<String, Object> getChartDataNew(String year) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        if(year == null||year.trim().equals("")){
            Calendar date = Calendar.getInstance();
            year = String.valueOf(date.get(Calendar.YEAR));
            //year = "2019";
        }

        List<Integer> membersData = memberService.getMemberCountMonthly(year);
        List<Integer> activityData = activityService.getActivityCountMonthly(year);
        List<Integer> couponData = couponService.getCouponCountMonthly(year);
        List<Integer> verfiData = couponService.getVerifCountMonthly(year);
        List<Integer> assessmentData = assessmentService.getAssessmentCountMonthly(year);

        String membersDataJson = "";
        for (int i = 0; i < membersData.size(); i++) {
            Integer data = membersData.get(i);
            membersDataJson += data + ((i+1) < membersData.size() ? "," : "");
        }

        String activityDataJson = "";
        for (int i = 0; i < activityData.size(); i++) {
            Integer data = activityData.get(i);
            activityDataJson += data + ((i+1) < activityData.size() ? "," : "");
        }

        String couponDataJson = "";
        for (int i = 0; i < couponData.size(); i++) {
            Integer data = couponData.get(i);
            couponDataJson += data + ((i+1) < couponData.size() ? "," : "");
        }

        String verfiDataJson = "";
        for (int i = 0; i < verfiData.size(); i++) {
            Integer data = verfiData.get(i);
            verfiDataJson += data + ((i+1) < verfiData.size() ? "," : "");
        }

        String assessmentDataJson = "";
        for (int i = 0; i < assessmentData.size(); i++) {
            Integer data = assessmentData.get(i);
            assessmentDataJson += data + ((i+1) < assessmentData.size() ? "," : "");
        }

        ModelAndView model = new ModelAndView();

        model.setViewName("datachart");

//        model.getModel().put("membersData", membersDataJson);
//        model.getModel().put("activityData", activityDataJson);
//        model.getModel().put("couponData", couponDataJson);
//        model.getModel().put("verfiData", verfiDataJson);
//        model.getModel().put("assessmentData", assessmentDataJson);

        resultMap.put("membersData", membersDataJson);
        // System.out.println(timesDataJson);
        resultMap.put("activityData", activityDataJson);
        resultMap.put("couponData", couponDataJson);
        resultMap.put("verfiData", verfiDataJson);
        resultMap.put("assessmentData", assessmentDataJson);

        return resultMap;
    }
}
